package com.base.dehong.http;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import android.content.Context;

/**
 * 网络的处理对象
 */
public class HttpModel {

	private Map<String, RequestQueue> mQueue = new HashMap<String, RequestQueue>();

	private static HttpModel instance = null;
	public static synchronized HttpModel getInstance(){
		if (instance == null) {
			instance = new HttpModel();
		}
		return instance;
	}

	private HttpModel() {
	}
	public void add(Request<?> re,Context context){
		String name = context.getClass().getName();
		RequestQueue requestQueue = mQueue.get(name);
		if (requestQueue == null) {
			requestQueue = Volley.newRequestQueue(context);
			mQueue.put(name, requestQueue);
		}
		requestQueue.add(re);
	}
	/**
	 * 删除，主要是处理当有一个Activity被删除后，它的所有网络请求都取消
	 * @param context
	 */
	public void remove(Context context){
		String name = context.getClass().getName();
		RequestQueue requestQueue = mQueue.get(name);
		if (requestQueue != null) {
			requestQueue.stop();
			mQueue.remove(name);
		}
	}
	public RequestQueue getRequestQueue(Context context){
		return mQueue.get(context.getClass().getName());
	}
}

